<template>
  <CommonCard title="今日交易" value="182,456">
    <div ref="barChartRef" style="width: 100%; height: 100%"></div>
    <template #footer>
      <span>退货率</span>
      <span class="emphasis">5.91%</span>
    </template>
  </CommonCard>
</template>

<script lang="ts">
import CommonCard from "./CommonCard.vue"
export default {
  name: "TodayUsers",
  components: { CommonCard },
}
</script>

<script lang="ts" setup>
import * as echarts from "echarts"
import { ref, onMounted } from "vue"
import type { EChartsType } from "echarts"

const barChartRef = ref(null)

let myChart: EChartsType

// 让代码在读取时不要执行，因为还没有拿到dome元素
// 所以包装成函数返回值的形式，在页面挂载完成时调用，获取数据
const getOption = () => {
  return {
    xAxis: {
      boundaryGap: false, // 两侧不留空白
      data: [
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月",
      ], // x轴数据列表
    },
    yAxis: {
      show: false,
    },
    series: [
      {
        type: "bar",
        data: [44, 126, 55, 88, 133, 99, 33, 113, 63, 98, 77, 123],
      },
    ],
    grid: {
      left: "10px",
      right: "10px",
    },
    tooltip: {
      trigger: "axis",
      confine: true,
    },
  }
}

// 1. 当页面组件挂载完毕的时候 才要使用数据生成对应的图形
onMounted(() => {
  myChart = echarts.init(barChartRef.value)
  myChart.setOption(getOption())

  // 当浏览器宽度发生变化的时候，图表也要发生变化
  window.addEventListener("resize", () => {
    myChart && myChart.resize()
  })
})
</script>

<style lang="scss" scoped></style>
